<?php

date_default_timezone_set('Asia/Shanghai');

function __autoload($className) {

	$classFile = dirname(__DIR__) . DIRECTORY_SEPARATOR . "client" . DIRECTORY_SEPARATOR . $className . ".php";
	if (is_file($classFile) && file_exists($classFile)) {
		require_once $classFile;
	}

	return;
}

$config = json_decode(file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . "etc" . DIRECTORY_SEPARATOR . "main.json"));

// try {
// 	$config->localdb->conn = new SimpleSSDB($config->localdb->host, $config->localdb->port);
// 	$config->remotedb->conn = new Redis;
// } catch (Exception $e) {
// 	die("Initialize db connector error:%s\n" . $e->getMessage());
// }

function initLocalDBConn() {
	global $config;

	for ($try = 0; $try < 3; $try++) {
		try {
			// $config->localdb->conn = new SimpleSSDB($config->localdb->host, $config->localdb->port);
			// $config->localdb->conn->auth($config->localdb->auth);

			$config->localdb->conn = new Redis();
			$config->localdb->conn->pconnect($config->localdb->host, $config->localdb->port, 3);
			$config->localdb->conn->auth($config->localdb->auth);
		} catch (Exception $e) {
			printf("%s line:%s initLocalDBConn error:%s\n", date("Y-m-d H:i:s"), __LINE__, $e->getMessage());
			sleep(1);
			continue;
		}

		// sleep(2);
		return true;
	}

	return false;
}

function initRemoteDBConn() {
	global $config;

	for ($try = 0; $try < 3; $try++) {
		try {
			$config->remotedb->conn = new Redis();
			$config->remotedb->conn->pconnect($config->remotedb->host, $config->remotedb->port);
			$config->remotedb->conn->auth($config->remotedb->auth);
		} catch (Exception $e) {
			printf("%s line:%s initRemoteDBConn error:%s\n", date("Y-m-d H:i:s"), __LINE__, $e->getMessage());
			sleep(1);
			continue;
		}

		// sleep(2);
		return true;
	}

	return false;
}

/*function initSSDBConn() {
	global $config;

	for ($try = 0; $try < 3; $try++) {
		try {
			$config->ssdb->conn = new SSDB();
			$config->ssdb->conn->pconnect($config->ssdb->host, $config->ssdb->port);
		} catch (Exception $e) {
			printf("%s line:%s initSSDBConn error:%s\n", date("Y-m-d H:i:s"), __LINE__, $e->getMessage());
			sleep(1);
			continue;
		}

		// sleep(2);
		return true;
	}

	return false;
}*/


